<template>
	<view>
	</view>
</template>

<script>
	import * as SystemJs from '@/common/system.js'
	export default {
		data() {
			return {
				barCode: undefined,
				open: false,
				isContinuScan: false //是否连续扫描
			}
		},
		onLoad(res) {
			var isContinuScan = uni.getStorageSync('isContinuScan');
			if (isContinuScan) {
				this.isContinuScan = Boolean(isContinuScan);
			}

			// #ifdef APP-PLUS
			var trsObj = JSON.parse(res.objStr)
			if (trsObj.isContinuScan !== undefined) {
				this.isContinuScan = trsObj.isContinuScan
			}
			// plus.navigator.setFullscreen(true); //全屏
			///获取当前打开的所有界面数组
			var pages = getCurrentPages();
			//获取当前打开的页面的路由地址
			var currentPath = pages[pages.length - 1];
			var webView = currentPath.$getAppWebview();
			this.scan(webView)
			this.createView(webView)
			// this.init();
			// #endif
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: true
			});
		},
		methods: {
			scan(webView) {
				try {
					// #ifdef APP-PLUS
					if (this.barCode == undefined) {
						this.barCode = plus.barcode.create('barCodeView',
							[plus.barcode.QR, plus.barcode.CODE128], {
								frameColor: '#55aa00',
								background: 'rgba(5,170,0,0)',
								top: '0px',
								left: '0px',
								width: '100%',
								height: '100%',
								scanbarColor: '#55aa00'
							}, true);
						console.log(typeof(this.barCode))
						//扫码成功事件
						this.barCode.onmarked = this.onmarked;
						//扫码失败事件
						this.barCode.onerror = this.onerror;

						//是否开启闪关灯
						this.barCode.setFlash(this.open);
						webView.append(this.barCode);
					}
					if (this.barCode != undefined) {
						console.log(this.barCode)
						//开始扫码识别
						this.barCode.start();
						var b = plus.barcode.getBarcodeById('barCodeView')

						// b.start()
						console.log(b)
					}
					// #endif
				} catch (e) {
					uni.$emit('scanBarFail', e.message)
					this.back();
					//TODO handle the exception
				}
			},
			//扫描成功的函数
			onmarked(type, result) {
				var text = '未知: ';
				switch (type) {
					case plus.barcode.QR:
						text = 'QR: ';
						break;
					case plus.barcode.EAN13:
						text = 'EAN13: ';
						break;
					case plus.barcode.EAN8:
						text = 'EAN8: ';
						break;
				}
				console.log(text + result)
				var obj = {}
				obj.type = text;
				obj.result = result;
				obj.callback = this.reStartScan;
				uni.$emit('scanBarSuccess', obj)

				//调用成功后的回调函数
				// SystemJs.unitJs.scanCallBack(obj);
				//实现连续扫描

			},
			reStartScan() {
				if (this.isContinuScan) {
					setTimeout(() => {
						if (this.barCode) {
							//开始扫码识别
							this.barCode.start()
							SystemJs.unitJs.reStartScan();
						}
					}, 1500)
				} else {
					this.back()
				}
			},
			//扫描失败事件
			onerror(err) {
				uni.$emit('scanBarFail', err)
				this.back();
			},
			//自定义创建原生页面控件
			createView(webView) {
				// #ifdef APP-PLUS
				//创建原生控件-右箭头
				var backView = new plus.nativeObj.View('backView', {
					top: '50px',
					left: '10px',
					height: '40px',
					width: '20%'
				}, [{
					tag: 'img',
					id: 'backBar',
					src: '_www/static/images/right.png',
					position: {
						top: '2px',
						left: '3px',
						width: '30px',
						height: '30px'
					}
				}]);

				// 创建打开手电筒的按钮
				var scanBarVew = new plus.nativeObj.View('scanBarVew', {
					top: '60%',
					left: '40%',
					height: '10%',
					width: '20%'
				}, [{
					tag: 'img',
					id: 'scanBar',
					src: '_www/static/images/shoudiantong.png',
					position: {
						width: '28%',
						left: '36%',
						height: '30%'
					}
				}, {
					tag: 'font',
					id: 'font',
					text: '轻触照亮',
					textStyles: {
						size: '15px',
						color: '#ffffff'
					},
					position: {
						width: '80%',
						left: '10%'
					}
				}]);
				var content = new plus.nativeObj.View('content', {
					top: '50px',
					left: '0px',
					height: '100%',
					width: '100%'
				}, [{
					tag: 'font',
					id: 'scanTitle',
					text: '扫一扫',
					textStyles: {
						size: '18px',
						color: '#ffffff'
					},
					position: {
						top: '0px',
						left: '0px',
						width: '100%',
						height: '40px'
					}
				}])
				backView.interceptTouchEvent(true)
				scanBarVew.interceptTouchEvent(true)
				//由于原生的视图是叠加的，所以顺序不能颠倒否则会导致无法显示后续样式
				webView.append(content);
				webView.append(backView);
				webView.append(scanBarVew);

				//返回按钮点击事件
				backView.addEventListener("click", (e) => {
					this.back();
				})
				//打开闪光灯的事件
				var that = this;
				scanBarVew.addEventListener('click', (e) => {
					that.open = !that.open;
					if (!that.open) {
						scanBarVew.draw([{
							tag: 'img',
							id: 'scanBar',
							src: '_www/static/images/shoudiantong.png',
							position: {
								width: '28%',
								left: '36%',
								height: '30%'
							}
						}, {
							tag: 'font',
							id: 'font',
							text: '轻触照亮',
							textStyles: {
								size: '15px',
								color: '#ffffff'
							},
							position: {
								width: '80%',
								left: '10%'
							}
						}])
					} else {
						scanBarVew.draw([{
							tag: 'img',
							id: 'scanBar',
							src: '_www/static/images/shoudiantong.png',
							position: {
								width: '28%',
								left: '36%',
								height: '30%'
							}
						}, {
							tag: 'font',
							id: 'font',
							text: '轻触关闭',
							textStyles: {
								size: '15px',
								color: '#55aa00'
							},
							position: {
								width: '80%',
								left: '10%'
							}
						}])
					}
					//修改闪光灯打开状态
					if (this.barCode) {
						this.barCode.setFlash(that.open)
					}
				})

				// #endif
			},
			back() {
				//移除扫码监听事件
				uni.$off('scanBarSuccess')
				uni.$off('scanBarFail')
				if (this.barCode) {
					//关闭条码识别控件
					this.barCode.close()
				}
				// 关闭保持屏幕常亮
				uni.setKeepScreenOn({
					keepScreenOn: false
				});
				let canNavBack = getCurrentPages()
				if (canNavBack && canNavBack.length > 1) {
					uni.navigateBack({
						delta: 1
					})
				} else {
					//如果刷新页面当前页面的路由信息会丢失，所以只能通过原生js的方法退回上一页面
					history.back();
				}
			}
		},
		/**
		 * 返回按钮监听事件
		 */
		onBackPress() {
			//移除扫码监听事件
			uni.$off('scanBarSuccess')
			uni.$off('scanBarFail')
			if (this.barCode) {
				//关闭条码识别控件
				this.barCode.close()
			}
			// 关闭保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: false
			});
		}
	}
</script>

<style>

</style>